// 飞机飞行距离
function distanceOfPlane(planeA, planeB) {
  return (planeA.x - planeB.x) * (planeA.x - planeB.x) + (planeA.y - planeB.y) * (planeA.y - planeB.y) + (planeA.z - planeB.z) * (planeA.z - planeB.z)
}

// 检测距离是否小于安全距离
export function checkCrash(planeArr, safe) {
  for (let i = 0; i < planeArr.length; ++i) {
    for (let j = 0; j < i; ++j) {
      let dis = distanceOfPlane(planeArr[i], planeArr[j]);
      if (dis <= safe * safe) {
        return {
          crash: true,
          index1: i,
          index2: j
        }
      }
    }
  }
  return {
    crash: false,
    index1: -1,
    index2: -1
  };
}

export function checkH(planeArr) {
  planeArr.sort(function (a, b) {
    if (a.x !== b.x) return a.x < b.x;
    if (a.y !== b.y) return a.y < b.y;
    else return a.z < b.z;
  })
  // 最左侧五架飞机x坐标相差不超过100 z坐标相差不超过100
  for (let now = 0; now < 4; ++now) {
    let next = now + 1;
    let gapOfX = Math.abs(planeArr[now].x - planeArr[next].x);
    let gapOfZ = Math.abs(planeArr[now].z - planeArr[next].z);
    if (gapOfX > 100 || gapOfZ > 100) {
      return false;
    }
  }
  // 最中间三架飞机y坐标相差不超过100 z坐标相差不超过100
  for (let now = 5; now < 7; ++now) {
    let next = now + 1;
    let gapOfY = Math.abs(planeArr[now].y - planeArr[next].y);
    let gapOfZ = Math.abs(planeArr[now].z - planeArr[next].z);
    if (gapOfY > 100 || gapOfZ > 100) {
      console.log(2);
      return false;
    }
  }
  // 最右侧五架飞机x坐标相差不超过100 z坐标相差不超过100
  for (let now = 8; now < 12; ++now) {
    let next = now + 1;
    let gapOfX = Math.abs(planeArr[now].x - planeArr[next].x);
    let gapOfZ = Math.abs(planeArr[now].z - planeArr[next].z);
    if (gapOfX > 100 || gapOfZ > 100) {
      return false;
    }
  }
  return true;
}
